var Api = require('../../utils/api.js');
var util = require('../../utils/util.js');
var md5 = require('../../utils/md5.js'); 
var ti;
Page({
  data: {
    phone: '',
    code: '',
    sended: false,
    doing: false,
    codeText: '获取验证码',
    disabled: false,
    loading: false
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  bindKeyInput: function (e) {
    var id = e.currentTarget.id;
    if (id == 'phone') {
      this.setData({
        phone: e.detail.value
      });
    } else if (id == 'code') {
      this.setData({
        code: e.detail.value
      })
    }
  },
  // 获取验证码
  getCode: function (obj) {
    var self = this;
    console.log('getCode - ');
    if (self.data.sended) {
      return;
    }
    var phone = this.data.phone;
    console.log('phone - ' + phone);
    if (phone === '') {
      wx.showModal({
        title: '提示',
        content: '请输入手机号',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
      return;
    }
    self.data.sended = true;
    var timestamp = new Date().getTime(); 
    var uniCode = timestamp;
    var verify = md5.hexMD5(timestamp + 'raylihzp') ;
    wx.showNavigationBarLoading();
    wx.request({
      url: Api.getSmsCode(),
      method: 'POST',
      data: {
        mobile: phone,
        uc:uniCode,
        ts:timestamp,
        h:verify
      },
      header: Api.headers,
      success: function (res) {
        wx.hideNavigationBarLoading();
        var data = res.data;
        if (data.code != 1) {
          self.data.sended = false;
          return;
        }
        if (data.code = 1) { 
          wx.showToast({
            title: data.msg,
            duration: 500
          })
        }
        self.setData({
          codeText: '100秒'
        });
        var timer = 99;
        var ti = setInterval(function () {
          self.setData({
            codeText: timer + '秒'
          });
          timer = timer - 1;
          if (timer < 0) {
            self.data.sended = false;
            self.setData({
              codeText: '获取验证码'
            });
            clearInterval(ti);
          }
        }, 1000);
      },
      fail: function (res) {
        wx.hideNavigationBarLoading();
        self.data.sended = false;
        wx.showModal({
          title: '提示',
          content: '服务器异常<br>请稍后重试',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            }
          }
        });
      },
    });

  },

  // 提交注册信息
  submit: function (obj) {
    var self = this;
    console.log('submit - ');
    if (this.data.doing) {
      return;
    }
    var phone = this.data.phone;
    var code = this.data.code;;
    console.log('phone - ' + phone + '  code - ' + code);
    if (phone === '') {
      wx.showModal({
        title: '提示',
        content: '请输入手机号',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
      return;
    }
    if (code === '') {
      wx.showModal({
        title: '提示',
        content: '请输入验证码',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
      return;
    }
    self.data.sended = true;
    self.setData({
      disabled: true,
      loading: true
    });
    wx.request({
      url: Api.mobileLogin,
      method: 'POST',
      data: {
        mobile: phone,
        code: code
      },
      header: Api.headers,
      success: function (res) {
        self.setData({
          disabled: false,
          loading: false
        });
        console.log(phone + "-" + code + "-" + JSON.stringify(res));
        var data = res.data;

        if (data.code != 1 || !data.data) {

          wx.showModal({
            title: '提示',
            content: data.msg,
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              }
            }
          });
          self.data.sended = false;
          return;
        }
        if (!data.data.username) {
          self.submit();
          return;
        }
        wx.showToast({
          title: '登录成功',
          duration: 500
        })

        wx.setStorage({
          key: 'logined',
          data: 'done'
        });

        var user = wx.getStorageSync('usr');
        if (user) {
          // $api.rmStorage('usr');
        }
        user = {};
        user = data.data;

        console.log("setStorage" + "-" + JSON.stringify(user));
        wx.setStorage({
          key: 'user',
          data: user
        });

        wx.navigateBack();

      },
      fail: function (res) {
        self.setData({
          disabled: false,
          loading: false
        });
        self.data.sended = false;
        wx.showToast({
          title: '服务器异常<br>请稍后重试',
          icon: 'loading',
          duration: 500
        })
      },
    });
  }

})